home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / IISAPP.VB_ / iisapp.vbs
Encoding:
Text File  |  2003-02-21  |  9.0 KB  |  297 lines

  1. '
  2. ' Copyright (c) Microsoft Corporation.  All rights reserved.
  3. '
  4. ' VBScript Source File 
  5. '
  6. ' Script Name: IIsApp.vbs
  7. '
  8.  
  9. Option Explicit
  10. On Error Resume Next
  11.  
  12. ' Error codes
  13. Const ERR_OK              = 0
  14. Const ERR_GENERAL_FAILURE = 1
  15.  
  16. '''''''''''''''''''''
  17. ' Messages
  18. Const L_Gen_ErrorMessage               = "%1 : %2"
  19. Const L_CmdLib_ErrorMessage            = "Could not create an instance of the CmdLib object."
  20. Const L_ChkCmdLibReg_ErrorMessage      = "Please register the Microsoft.CmdLib component."
  21. Const L_ScriptHelper_ErrorMessage      = "Could not create an instance of the IIsScriptHelper object."
  22. Const L_ChkScpHelperReg_ErrorMessage   = "Please, check if the Microsoft.IIsScriptHelper is registered."
  23. Const L_InvalidSwitch_ErrorMessage     = "Invalid switch: %1"
  24. Const L_NotEnoughParams_ErrorMessage   = "Not enough parameters."
  25. Const L_Query_ErrorMessage             = "Error executing query"
  26. Const L_Serving_Message                = "The following W3WP.exe processes are serving AppPool: %1"
  27. Const L_NoW3_ErrorMessage              = "Error - no w3wp.exe processes are running at this time"
  28. Const L_PID_Message                    = "W3WP.exe PID: %1"
  29. Const L_NoResults_ErrorMessage         = "Error - no results"
  30. Const L_APID_Message                   = "W3WP.exe PID: %1   AppPoolId: %2"
  31. Const L_NotW3_ErrorMessage             = "ERROR: ProcessId specified is NOT an instance of W3WP.exe - EXITING"
  32. Const L_PIDNotValid_ErrorMessage       = "ERROR: PID is not valid"
  33.  
  34. '''''''''''''''''''''
  35. ' Help
  36. Const L_Empty_Text     = ""
  37.  
  38. ' General help messages
  39. Const L_SeeHelp_Message         = "Type IIsApp /? for help."
  40.  
  41. Const L_Help_HELP_General01_Text = "Description: list IIS worker processes."
  42. Const L_Help_HELP_General02_Text = "Syntax: IIsApp.vbs [/a <app_pool_id> | /p <pid>]"
  43. Const L_Help_HELP_General04_Text = "Parameters:"
  44. Const L_Help_HELP_General05_Text = ""
  45. Const L_Help_HELP_General06_Text = "Value              Description"
  46. Const L_Help_HELP_General07_Text = "/a <app_pool_id>   report PIDs of currently running" 
  47. Const L_Help_HELP_General08_Text = "                   w3wp.exe processes serving pool" 
  48. Const L_Help_HELP_General09_Text = "                   <app_pool_id>.  Surround <app_pool_id>"
  49. Const L_Help_HELP_General10_Text = "                   with quotes if it contains spaces."
  50. Const L_Help_HELP_General11_Text = "                   not specified by default and exclusive"
  51. Const L_Help_HELP_General12_Text = "                   to /p switch."
  52. Const L_Help_HELP_General13_Text = "/p <pid>           report AppPoolId of w3wp specified by <pid>"
  53. Const L_Help_HELP_General14_Text = "DEFAULT: no switches will print out the PID and AppPoolId"
  54. Const L_Help_HELP_General15_Text = "         for each w3wp.exe"
  55.  
  56. ''''''''''''''''''''''''
  57. ' Operation codes
  58. Const OPER_BY_NAME = 1
  59. Const OPER_BY_PID  = 2
  60. Const OPER_ALL     = 3
  61.  
  62. '
  63. ' Main block
  64. '
  65. Dim oScriptHelper, oCmdLib
  66. Dim intOperation, intResult
  67. Dim strCmdLineOptions
  68. Dim oError
  69. Dim aArgs
  70. Dim apoolID, PID
  71. Dim oProviderObj
  72.  
  73. ' Default
  74. intOperation = OPER_ALL
  75. Const wmiConnect  = "winmgmts:{(debug)}:/root/cimv2"
  76. Const queryString = "select * from Win32_Process where Name='w3wp.exe'"
  77. Const pidQuery    = "select * from Win32_Process where ProcessId="
  78.  
  79. ' get NT WMI provider
  80. Set oProviderObj = GetObject(wmiConnect)
  81.  
  82. ' Instantiate the CmdLib for output string formatting
  83. Set oCmdLib = CreateObject("Microsoft.CmdLib")
  84. If Err.Number <> 0 Then
  85.     WScript.Echo L_CmdLib_ErrorMessage
  86.     WScript.Echo L_ChkCmdLibReg_ErrorMessage    
  87.     WScript.Quit(ERR_GENERAL_FAILURE)
  88. End If
  89. Set oCmdLib.ScriptingHost = WScript.Application
  90.  
  91. ' Instantiate script helper object
  92. Set oScriptHelper = CreateObject("Microsoft.IIsScriptHelper")
  93. If Err.Number <> 0 Then
  94.     WScript.Echo L_ScriptHelper_ErrorMessage
  95.     WScript.Echo L_ChkScpHelperReg_ErrorMessage    
  96.     WScript.Quit(ERR_GENERAL_FAILURE)
  97. End If
  98.  
  99. Set oScriptHelper.ScriptHost = WScript
  100.  
  101. ' Check if we are being run with cscript.exe instead of wscript.exe
  102. oScriptHelper.CheckScriptEngine
  103.  
  104. ' Command Line parsing
  105. Dim argObj, arg
  106. Set argObj = WScript.Arguments
  107.  
  108. strCmdLineOptions = "a:a:1;p:p:1"
  109.  
  110. If argObj.Named.Count > 0 Then
  111.     Set oError = oScriptHelper.ParseCmdLineOptions(strCmdLineOptions)
  112.  
  113.     If Not oError Is Nothing Then
  114.         If oError.ErrorCode = oScriptHelper.ERROR_NOT_ENOUGH_ARGS Then
  115.             ' Not enough arguments for a specified switch
  116.             WScript.Echo L_NotEnoughParams_ErrorMessage
  117.             WScript.Echo L_SeeHelp_Message
  118.         Else
  119.             ' Invalid switch
  120.             oCmdLib.vbPrintf L_InvalidSwitch_ErrorMessage, Array(oError.SwitchName)
  121.               WScript.Echo L_SeeHelp_Message
  122.         End If
  123.         
  124.             WScript.Quit(ERR_GENERAL_FAILURE)
  125.     End If
  126.  
  127.     If oScriptHelper.GlobalHelpRequested Then
  128.         DisplayHelpMessage
  129.         WScript.Quit(ERR_OK)
  130.     End If
  131.  
  132.     For Each arg In oScriptHelper.Switches
  133.         Select Case arg
  134.             Case "a"
  135.                 apoolID = oScriptHelper.GetSwitch(arg)
  136.                 intOperation = OPER_BY_NAME
  137.             Case "p"
  138.                 PID = oScriptHelper.GetSwitch(arg)
  139.                 intOperation = OPER_BY_PID
  140.         End Select
  141.     Next
  142.  
  143. End If
  144.  
  145. ' Choose operation
  146. Select Case intOperation
  147.     Case OPER_BY_NAME
  148.         intResult = GetByPool(apoolID)
  149.         
  150.     Case OPER_BY_PID
  151.         intResult = GetByPid(PID)
  152.  
  153.     Case OPER_ALL
  154.         intResult = GetAllW3WP()
  155. End Select
  156.  
  157. ' Return value to command processor
  158. WScript.Quit(intResult)
  159.  
  160. '''''''''''''''''''''''''
  161. ' End Of Main Block
  162. '''''''''''''''''''''
  163.  
  164. '''''''''''''''''''''''''''
  165. ' DisplayHelpMessage
  166. '''''''''''''''''''''''''''
  167. Sub DisplayHelpMessage()
  168.     WScript.Echo L_Help_HELP_General01_Text
  169.     WScript.Echo L_Empty_Text
  170.     WScript.Echo L_Help_HELP_General02_Text
  171.     WScript.Echo L_Empty_Text
  172.     WScript.Echo L_Help_HELP_General04_Text
  173.     WScript.Echo L_Help_HELP_General05_Text
  174.     WScript.Echo L_Help_HELP_General06_Text
  175.     WScript.Echo L_Help_HELP_General07_Text
  176.     WScript.Echo L_Help_HELP_General08_Text
  177.     WScript.Echo L_Help_HELP_General09_Text
  178.     WScript.Echo L_Help_HELP_General10_Text
  179.     WScript.Echo L_Help_HELP_General11_Text
  180.     WScript.Echo L_Help_HELP_General12_Text
  181.     WScript.Echo L_Help_HELP_General13_Text
  182.     WScript.Echo L_Empty_Text
  183.     WScript.Echo L_Help_HELP_General14_Text
  184.     WScript.Echo L_Help_HELP_General15_Text
  185. End Sub
  186.  
  187. Function GetAppPoolId(strArg)
  188.     Dim Submatches
  189.     Dim strPoolId
  190.     Dim re
  191.     Dim Matches
  192.  
  193.     On Error Resume Next
  194.  
  195.     Set re = New RegExp
  196.     re.Pattern = "-ap ""(.+)"""
  197.     re.IgnoreCase = True
  198.     Set Matches = re.Execute(strArg)
  199.     Set SubMatches = Matches(0).Submatches
  200.     strPoolId = Submatches(0)
  201.     
  202.     GetAppPoolId = strPoolId
  203. End Function
  204.  
  205.  
  206. Function GetByPool(strPoolName)
  207.     Dim W3WPList
  208.     Dim strQuery
  209.     Dim W3WP
  210.  
  211.     On Error Resume Next
  212.  
  213.     strQuery = queryString
  214.     Set W3WPList = oProviderObj.ExecQuery(strQuery)
  215.     If (Err.Number <> 0) Then
  216.         WScript.Echo L_Query_ErrorMessage
  217.         oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  218.         GetByPid = 2
  219.     Else
  220.         oCmdLib.vbPrintf L_Serving_Message, Array(strPoolName)
  221.         If (W3WPList.Count < 1) Then
  222.             WScript.Echo L_NoW3_ErrorMessage
  223.             GetByPool = 1
  224.         Else
  225.             For Each W3WP In W3WPList
  226.                 If (UCase(GetAppPoolId(W3WP.CommandLine)) = UCase(strPoolName)) Then
  227.                     oCmdLib.vbPrintf L_PID_Message, Array(W3WP.ProcessId)
  228.                 End If
  229.             Next
  230.             GetByPool = 0
  231.         End If
  232.     End If
  233. End Function
  234.  
  235. Function GetByPid(pid)
  236.     Dim W3WPList
  237.     Dim strQuery
  238.     Dim W3WP
  239.  
  240.     On Error Resume Next
  241.  
  242.     If (IsNumeric(pid)) Then
  243.         strQuery = pidQuery & pid
  244.     
  245.         Set W3WPList = oProviderObj.ExecQuery(strQuery)
  246.         If (Err.Number <> 0) Then
  247.             WScript.Echo L_Query_ErrorMessage
  248.             oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  249.             GetByPid = 2
  250.         Else
  251.             If (W3WPList.Count < 1) Then
  252.                 WScript.Echo L_NoResults_ErrorMessage
  253.                 GetByPid = 2
  254.             Else
  255.                 For Each W3WP In W3WPList
  256.                     If (W3WP.Name = "w3wp.exe") Then
  257.                         oCmdLib.vbPrintf L_APID_Message, Array(pid, GetAppPoolId(W3WP.CommandLine))
  258.                         GetByPid = 0
  259.                     Else
  260.                         WScript.Echo(L_NotW3_ErrorMessage)
  261.                         GetByPid = 2
  262.                     End If
  263.                 Next
  264.             End If
  265.         End If
  266.     Else
  267.         WScript.Echo(L_PIDNotValid_ErrorMessage)
  268.         GetByPid = 2
  269.     End If
  270. End Function
  271.         
  272. Function GetAllW3WP()
  273.     Dim W3WPList
  274.     Dim strQuery
  275.     Dim W3WP
  276.  
  277.     On Error Resume Next
  278.  
  279.     strQuery = queryString
  280.     Set W3WPList = oProviderObj.ExecQuery(strQuery)
  281.     If (Err.Number <> 0) Then
  282.         WScript.Echo L_Query_ErrorMessage
  283.         oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  284.         GetByPid = 2
  285.     Else
  286.         If (W3WPList.Count < 1) Then
  287.             WScript.Echo L_NoResults_ErrorMessage
  288.             GetAllW3WP = 2
  289.         Else
  290.             For Each W3WP In W3WPList
  291.                 oCmdLib.vbPrintf L_APID_Message, Array(W3WP.ProcessId, GetAppPoolId(W3WP.CommandLine))
  292.             Next
  293.             GetAllW3WP = 0
  294.         End If
  295.     End If
  296. End Function
  297.